package com.ahui.ydd.service.impl;

import com.ahui.ydd.common.CustomException;
import com.ahui.ydd.entity.YddUser;
import com.ahui.ydd.mapper.YddUserMapper;
import com.ahui.ydd.service.YddUserService;
import com.ahui.ydd.utils.JwtUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
@Slf4j
public class YddUserServiceImpl extends ServiceImpl<YddUserMapper, YddUser> implements YddUserService {

      //获取当前用户id
    public Integer getUserId(HttpServletRequest request){
        String token = request.getHeader("token");

        boolean flag = JwtUtils.verifyToken(token);
        if (!flag) {
            throw new CustomException("token已失效");
        }
        Claims claims = JwtUtils.getClaims(token);
        return (Integer) claims.get("id");
    }
}
